<?php
namespace app\home\controller;

use app\common\model\VideoNew as VideoNewModel;
use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\NewsLike as NewsLikeModel;
use app\common\model\VideoEpisode as VideoEpisodeModel;

/**
 * 佛教视频控制器
 * Class Videonew
 * @package app\index\controller
 */
class Videonew extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new VideoNewModel;
        $cmodel = new NewsCommentModel;
        $lmodel = new NewsLikeModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            if (!empty($param['cid'])) {
                $map['cid'] = $param['cid'];
            }
            $map['states'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size, 'id,title,cover,share_num,label,create_time');
            foreach($lists as &$v){
                $v['label'] = explode(',',$v['label']);
                $comment_num = $cmodel->getAllCount(['nid'=>$v['id'],'status'=>1,'mtype'=>2]);
                $v['comment_num'] = $comment_num;
                $v['like_num'] = $lmodel->getAllCount(['nid'=>$v['id'],'status'=>1,'mtype'=>2]);
                $like = $lmodel->findOne(['nid'=>$v['id'],'mid'=>$user['id'],'status'=>1,'mtype'=>2]);
                if(empty($like)){
                    $v['is_like'] = 2;
                }else{
                    $v['is_like'] = 1;
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function detial()
    {
        $param = input('param.');
        $model = new VideoNewModel;
        $lmodel = new NewsLikeModel;
        $emodel = new VideoEpisodeModel;
        $user = $this->user;
        try{
            $data = $model->findData($param['id'],'id','id,title,cover,label,from,create_time');
            $data['like_num'] = $lmodel->getAllCount(['nid'=>$data['id'],'status'=>1,'mtype'=>2]);
            $like = $lmodel->findOne(['nid'=>$param['id'],'mid'=>$user['id'],'status'=>1,'mtype'=>2]);
            if(empty($like)){
                $data['is_like'] = 2;
            }else{
                $data['is_like'] = 1;
            }
            $data['episode'] = $emodel->getAllData(['vid'=>$param['id'],'states'=>1],'sort','id,title,number,url','asc');
            $data['label'] = explode(',',$data['label']);
            add_quality($user['id'],31,$user['quality'],4);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}